package com.healthcloud.controller;

import com.healthcloud.service.TestResultService;
import com.healthcloud.utils.ApiRes;
import com.healthcloud.utils.JwtUtil;
import com.healthcloud.utils.PageParam;
import com.healthcloud.utils.Pagine;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xuzheng
 * @date 2019/2/20 8:43
 */
@Api(value = "月度报表")
@RestController
@RequestMapping("/mp/monthly/report")
public class MonthlyReportController {

//    @Value("${testResult.normal}")
    private Integer normal=0;

//    @Value("${testResult.unusual}")
    private Integer unusual=0;

    @Autowired
    private TestResultService testResultService;


    /**
     * 血压检测次数
     *
     * @param userVO 用户信息
     * @return map
     */
    @ApiOperation(value = "血压检测次数")
    @GetMapping("/bp/count")
    public ApiRes<Map<String, Object>> bpCount(@ApiParam(required = true, name = "reportTime", value = "月度报表时间", example = "2019-03")
                                               @RequestParam("reportTime") String reportTime, HttpServletRequest httpServletRequest) {
        String idCard = JwtUtil.getIdCardFromToken(JwtUtil.getTokenFromRequest(httpServletRequest));

        Integer total = testResultService.bpCount(normal, idCard, reportTime);
        Integer abnormal = testResultService.bpCount(unusual, idCard, reportTime);
        Map<String, Object> map = new HashMap<>(16);
        map.put("total", total);
        map.put("unusual", abnormal);
        return new ApiRes(true, map, null, null);
    }

    /**
     * 血压月度报表
     * <p> 返回值中error为异常状态 0:正常 1:偏低 2:偏高<p/>
     *
     * @param errorStatus 异常状态
     * @param reportTime  检测时间
     * @param userVO      用户信息
     * @return page
     */
    @ApiOperation(value = "血压月度报表")
    @GetMapping("/bp/info")
    public org.springframework.data.domain.Page<List> bpList(@ApiParam(name = "errorStatus", value = "异常状态：0为全部，1为异常")
                                                             @RequestParam(required = false, value = "errorStatus") Integer errorStatus,
                                                             @ApiParam(name = "reportTime", value = "检测时间", example = "2019-02")
                                                             @RequestParam(required = false, value = "reportTime") String reportTime,
                                                             PageParam pageParam, HttpServletRequest httpServletRequest) {
        String idCard = JwtUtil.getIdCardFromToken(JwtUtil.getTokenFromRequest(httpServletRequest));
        //默认检测时间为当前月份
        if (StringUtils.isEmpty(reportTime)) {
            Calendar calendar = Calendar.getInstance();
            String month = String.valueOf(calendar.get(Calendar.MONTH) + 1).length() == 2 ?
                    String.valueOf(calendar.get(Calendar.MONTH) + 1) : "0" + String.valueOf(calendar.get(Calendar.MONTH) + 1);
            reportTime = calendar.get(Calendar.YEAR) + "-" + month;
        }
        List list = testResultService.bpReport(errorStatus, reportTime, idCard, pageParam);
        Integer count = testResultService.bpCount(errorStatus, idCard, reportTime);
        Pagine pagine = new Pagine(pageParam.getiDisplayStart(), pageParam.getiDisplayLength());
        pagine.setContent(list);
        pagine.setTotal(count);
        return pagine;
    }


    /**
     * 血糖检测次数
     *
     * @param userVO 用户信息
     * @return map
     */
    @ApiOperation(value = "血糖检测次数")
    @GetMapping("/bs/count")
    public ApiRes<Map<String, Object>> bsCount(@ApiParam(required = true, name = "reportTime", value = "月度报表时间", example = "2019-03")
                                               @RequestParam("reportTime") String reportTime, HttpServletRequest httpServletRequest) {
        String idCard = JwtUtil.getIdCardFromToken(JwtUtil.getTokenFromRequest(httpServletRequest));
        Integer total = testResultService.bsCount(idCard, normal, reportTime);
        Integer abnormal = testResultService.bsCount(idCard, unusual, reportTime);
        Map<String, Object> map = new HashMap<>(16);
        map.put("total", total);
        map.put("unusual", abnormal);
        return new ApiRes(true, map, null, null);
    }


    /**
     * 血糖月度报表
     *
     * @param errorStatus 异常状态
     * @param reportTime  检测时间
     * @param userVO      用户信息
     * @param pageParam   分页
     * @return page
     */
    @ApiOperation(value = "血糖月度报表")
    @GetMapping("/bs/info")
    public org.springframework.data.domain.Page<List> bsList(@ApiParam(name = "errorStatus", value = "异常状态：0为全部，1为异常")
                                @RequestParam(required = false, value = "errorStatus") Integer errorStatus,
                                @ApiParam(name = "reportTime", value = "检测时间", example = "2019-02")
                                @RequestParam(required = false, value = "reportTime") String reportTime,
                                PageParam pageParam, HttpServletRequest httpServletRequest) {
        String idCard = JwtUtil.getIdCardFromToken(JwtUtil.getTokenFromRequest(httpServletRequest));
        //默认检测时间为当前月份
        if (StringUtils.isEmpty(reportTime)) {
            Calendar calendar = Calendar.getInstance();
            String month = String.valueOf(calendar.get(Calendar.MONTH) + 1).length() == 2 ?
                    String.valueOf(calendar.get(Calendar.MONTH) + 1) : "0" + String.valueOf(calendar.get(Calendar.MONTH) + 1);
            StringBuilder time = new StringBuilder().append(calendar.get(Calendar.YEAR)).append("-").append(month);
            reportTime = time.toString();
        }
        List list = testResultService.bsReport(errorStatus, reportTime, idCard, pageParam);
        Integer count = testResultService.bsCount(idCard, errorStatus, reportTime);
        Pagine pagine = new Pagine(pageParam.getiDisplayStart(), pageParam.getiDisplayLength());
        pagine.setContent(list);
        pagine.setTotal(count);
        return pagine;
    }


}
